package com.woniuxy.carlife.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.carlife.domain.ServiceGood;
import com.woniuxy.carlife.mapper.ServiceGoodMapper;
import com.woniuxy.carlife.minio.MinioTemplate;
import com.woniuxy.carlife.param.ServiceGoodParam;
import com.woniuxy.carlife.param.ServiceGoodPicParam;
import com.woniuxy.carlife.param.ServiceGoodPriceParam;
import com.woniuxy.carlife.service.ServiceGoodService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author fx
 * @ClassName:ServiceGoodServiceImpl
 * @Description: 保养商品service实现类
 * @date 2021/4/6 20:00
 **/

@Service
public class ServiceGoodServiceImpl extends ServiceImpl<ServiceGoodMapper, ServiceGood> implements ServiceGoodService {

    @Resource
    private MinioTemplate mt;

    @Resource
    private ServiceGoodMapper mapper;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 功能描述〈添加保养商品〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 10:04
     * @throws
     */
    @Override
    public int saveServiceGood(ServiceGoodParam serviceGood) throws IOException {
        //先根据名称查redis
        String s =(String) redisTemplate.opsForValue()
                .get("com.woniuxy:carlife:serviceproject:servicecgood:name:" + serviceGood.getServiceGoodName());
        if (ObjectUtils.isEmpty(s)){
            //根据名字查数据库
            ServiceGood good = mapper.selectOne(new QueryWrapper<ServiceGood>()
                    .eq("service_good_name", serviceGood.getServiceGoodName()));
            if (!ObjectUtils.isEmpty(good)){
                //如果数据库有数据就先把这个优惠券类别名存到redis
                redisTemplate.opsForValue()
                        .set("com.woniuxy:carlife:serviceproject:servicecgood:name:" + serviceGood.getServiceGoodName(),
                                serviceGood.getServiceGoodName());
                //设置过期时间,5分钟
                redisTemplate.expire("com.woniuxy:carlife:serviceproject:servicecgood:name:" + serviceGood.getServiceGoodName(),
                        5*60, TimeUnit.SECONDS);
                return -1;
            }
        }else{
            return -1;
        }
        ServiceGood good = new ServiceGood();
        //把参数属性设置到商品对象中
        //门店id
        good.setStoreId(serviceGood.getStoreId());
        //品牌型号
        good.setServiceGoodBrand(serviceGood.getServiceGoodBrand());
        //创建时间
        good.setGoodCreateTime(new Timestamp(new Date().getTime()));
        //商品类别
        good.setServiceGoodCategory(serviceGood.getServiceGoodCategory());
        //商品名
        good.setServiceGoodName(serviceGood.getServiceGoodName());
        //商品状态，默认为上架
        good.setServiceGoodStatus("上架");
        //商品价格
        good.setServiceGoodPrice(serviceGood.getServiceGoodPrice());
        //上传图片,单张
        good.setServiceGoodPic(serviceGood.getServiceGoodPic());
        //执行存储操作
        int insert = mapper.insert(good);
        if (insert>0){
            return 1;
        }
        return 0;
    }

    /**
     * 功能描述〈根据门店id查询所有商品〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 10:19
     * @throws
     */
    @Override
    public List<ServiceGood> findfindServiceGoodByStoreId(Integer storeId) {
        List<ServiceGood> goods = mapper.selectList
                (new QueryWrapper<ServiceGood>().eq("store_id", storeId));
        if (ObjectUtils.isEmpty(goods)){
            return null;
        }
        return goods;
    }

    /**
     * 功能描述〈根据门店id查询所有上架商品〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 10:20
     * @throws
     */
    @Override
    public List<ServiceGood> findServiceGoodByStatusAndStoreId(Integer storeId) {
        List<ServiceGood> goods = mapper.findServiceGoodByStatusAndStoreId(storeId);
        return goods;
    }

    /**
     * 功能描述〈根据商品id修改商品状态为下架〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 10:51
     * @throws
     */
    @Override
    public boolean updateServiceGoodStatusToDown(Integer goodId) {
        ServiceGood good = new ServiceGood();
        good.setServiceGoodId(goodId);
        good.setServiceGoodStatus("下架");
        int i = mapper.updateById(good);
        if (i>0){
            return true;
        }
        return false;
    }

    /**
     * 功能描述〈根据商品id修改商品价格〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 10:55
     * @throws
     */
    @Override
    public boolean updateServiceGoodPriceByGoodId(ServiceGoodPriceParam goodParam) {
        ServiceGood good = new ServiceGood();
        good.setServiceGoodId(goodParam.getGoodId());
        good.setServiceGoodPrice(goodParam.getPrice());
        int i = mapper.updateById(good);
        if (i>0){
            return true;
        }
        return false;
    }

    /**
     * 功能描述〈上传商品图片〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/13 3:40
     * @throws
     */
    @Override
    public String saveServiceGoodPic(MultipartFile pic) throws IOException {
        LocalDate id = LocalDate.now();
        StringBuffer fileName = new StringBuffer("/");
        fileName . append(id. getYear()) . append("/")
                . append(id. getMonthValue()). append("/")
                . append(id. getDayOfMonth()) . append("/")
                . append(System. currentTimeMillis());
        String type = pic. getOriginalFilename().
                substring(pic. getOriginalFilename().lastIndexOf("."));
        fileName . append(type);
        //上传后返回路径
        String url = mt.uploadFile(fileName.toString(), "fourstage", pic.getInputStream(), pic.getSize(), pic.getContentType());
        return url;
    }

    /**
     * 功能描述〈根据商品id和图片地址修改商品图片〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/14 16:00
     * @throws
     */
    @Override
    public boolean updateServiceGoodPicByGoodId(ServiceGoodPicParam goodPicParam) {
        ServiceGood good = new ServiceGood();
        good.setServiceGoodId(goodPicParam.getGoodId());
        good.setServiceGoodPic(goodPicParam.getPic());
        //执行操作
        int i = mapper.updateById(good);
        if (i>0){
            return true;
        }
        return false;
    }
}
